package com.google.gwt.mobile.phonegap.api;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.mobile.phonegap.DeviceType;
import com.google.gwt.mobile.phonegap.PhoneGap;

/**
 * The Class Network.
 */
public class Network {

  /**
   * Checks if is reachable.
   * 
   * @param reachableHostname the reachable hostname
   * @param callback the callback
   */
  public static void isReachable(final String reachableHostname, final Callback callback) {
    isReachable(reachableHostname, callback, (JavaScriptObject) null);
  }

  /**
   * Checks if is reachable.
   * 
   * @param reachableHostname the reachable hostname
   * @param callback the callback
   * @param options the options
   */
  public static void isReachable(final String reachableHostname, final Callback callback,
      final Options options) {
    if (PhoneGap.RUNTIME.getDeviceType() == DeviceType.IOS) {
      isReachableIOS(reachableHostname, callback, options.getOptions());
    } else {
      isReachable(reachableHostname, callback, options.getOptions());
    }
  }

  private native static void isReachableIOS(String reachableHostname, Callback callback,
      JavaScriptObject options) /*-{
		$wnd.navigator.network
				.isReachable(
						reachableHostname,
						function(reachability) {
							callback.@com.google.gwt.mobile.phonegap.api.Network.Callback::onNetworkStatus(I)(reachability.code);
						}, options);
  }-*/;

  private native static void isReachable(String reachableHostname, Callback callback,
      JavaScriptObject options) /*-{
		$wnd.navigator.network
				.isReachable(
						reachableHostname,
						function(reachability) {
							callback.@com.google.gwt.mobile.phonegap.api.Network.Callback::onNetworkStatus(I)(reachability);
						}, options);
  }-*/;

  /**
   * The Interface Callback.
   */
  public interface Callback {

    /**
     * On network status.
     * 
     * @param reachability the reachability
     */
    public void onNetworkStatus(int reachability);
  }

  /**
   * The Class NetworkStatus.
   */
  public static class NetworkStatus {

    /** The Constant NOT_REACHABLE. */
    public static final int NOT_REACHABLE = 0;

    /** The Constant REACHABLE_VIA_CARRIER_DATA_NETWORK. */
    public static final int REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;

    /** The Constant REACHABLE_VIA_WIFI_NETWORK. */
    public static final int REACHABLE_VIA_WIFI_NETWORK = 2;
  }

  /**
   * The Class Options.
   */
  public static class Options {
    Options self = this;
    JavaScriptObject options = JavaScriptObject.createObject();

    /**
     * Checks if is ip address.
     * 
     * @param b the b
     * @return the options
     */
    public native Options isIpAddress(boolean b) /*-{
			this.@com.google.gwt.mobile.phonegap.api.Network.Options::options.isIpAddress = b;
			return this.@com.google.gwt.mobile.phonegap.api.Network.Options::self;
    }-*/;

    private JavaScriptObject getOptions() {
      return options;
    }
  }
}
